package com.lightsaber.trade.as.service.device;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.lightsaber.trade.as.dao.device.DeviceDao;
import com.lightsaber.trade.as.entity.device.Device;
import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.entity.AuditedObject;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.service.BaseServiceImpl;

@Service
@Transactional
public class DeviceManager extends BaseServiceImpl<Device, String> {


    @Autowired
    DeviceDao deviceDao;
    @Override
    protected BaseDao<Device, String> getDao() {
        return deviceDao;
    }

    public Page<Device> findPage(final Device device, final Page<Device> page) {

        return deviceDao.findPage(device, page);
    }

    @Override
    public void save(final Device device) {

        if (StringUtils.isBlank(device.getId())) {

            device.setStatus("");
            deviceDao.save(device);

        } else {

            Device old = deviceDao.get(device.getId());

            BeanUtils.copyProperties(device, old, new String[] { BaseEntity.ID, BaseEntity.VERSION,
                    AuditedObject.CREATED_BY, AuditedObject.CREATED_DATE, "status" });

            deviceDao.save(old);
        }
    }
}
